package fr.imag.ufrima.ctte;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

import fr.imag.ufrima.ctte.events.ImagCtteListener;
import fr.imag.ufrima.ctte.events.ImagCtteEvent;
import fr.imag.ufrima.ctte.events.ImagCtteListener;
import fr.imag.ufrima.ctte.gui.windows.Main;

public class ImagCTTE {
	private List taskDiagrams;
	private TaskDiagram currentTaskDiagram;
	private int currentTaskDiagramNumber=0;
	private List listeners;						// Event listeners
	/**
	 * start method
	 * @param args
	 */
	public static void main(String[] args) {
		try {
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
	    } catch (InstantiationException e) {
	    } catch (ClassNotFoundException e) {
	    } catch (UnsupportedLookAndFeelException e) {
	    } catch (IllegalAccessException e) {}

		new Main(new ImagCTTE());
	}
	public ImagCTTE(){
		listeners = new ArrayList();
		taskDiagrams = new ArrayList();
		setCurrentTaskDiagram(null);
		
	}
	public void newTaskDiagram(){
		// create a new task diagram
		TaskDiagram taskDiagram = new TaskDiagram("Task Diagram " + currentTaskDiagramNumber++, 700,600);
		// add the new task diagram to the list
		taskDiagrams.add(taskDiagram);
		// fire New Task Diagram Event
		fireNewTaskDiagramEvent(taskDiagram);
		// Change the current Task Diagram to the new created one.
		setCurrentTaskDiagram(taskDiagram);
	}
	public void closeCurrentTaskDiagram(){
		taskDiagrams.remove(currentTaskDiagram);
		fireCloseTaskDiagramEvent(currentTaskDiagram);
		if (taskDiagrams.size() == 0){
			currentTaskDiagramNumber =0;	
			setCurrentTaskDiagram(null);
		}
		
	}
	public void closeAllTaskDiagrams(){
		for (int i=0; i<getTaskDiagramCount();i++){
			setCurrentTaskDiagram((TaskDiagram)taskDiagrams.get(getTaskDiagramCount()-1));
			closeCurrentTaskDiagram();
		}
		//setCurrentTaskDiagram(null);
	}
	public void Open(String file){
		
	}
	public void Save(String file){
		
	}
	public void quitApplication(){
		System.exit(-1);
	}
	public int getTaskDiagramCount(){
		return taskDiagrams.size(); 	
	}
	public TaskDiagram getCurrentTaskDiagram() {
		return currentTaskDiagram;
	}
	public void setCurrentTaskDiagram(TaskDiagram currentTaskDiagram) {
		this.currentTaskDiagram = currentTaskDiagram;
		fireTaskDiagramSwitchedEvent(this.currentTaskDiagram);
		
	}
	//============= EVENTS MANAGMENT ============================
	public synchronized void addImagCtteListener( ImagCtteListener l ) {
        listeners.add(l);
    }    
    public synchronized void removeImagCtteListener( ImagCtteListener l ) {
        listeners.remove(l);
    }
	//============= FIRE EVENTS =================================
	private synchronized void fireNewTaskDiagramEvent(TaskDiagram taskDiagram) {
        ImagCtteEvent event = new ImagCtteEvent(this, taskDiagram);
		Iterator it = listeners.iterator();
        while( it.hasNext()){
        	((ImagCtteListener) it.next()).newTaskDiagram(event);
        }
    }
	private synchronized void fireCloseTaskDiagramEvent(TaskDiagram taskDiagram) {
        ImagCtteEvent event = new ImagCtteEvent(this, taskDiagram);
		Iterator it = listeners.iterator();
        while( it.hasNext()){
        	((ImagCtteListener) it.next()).closeTaskDiagram(event);
        }
    }
	private synchronized void fireTaskDiagramSwitchedEvent(TaskDiagram taskDiagram) {
		ImagCtteEvent event = new ImagCtteEvent(this, taskDiagram);
		Iterator it = listeners.iterator();
        while( it.hasNext()){
        	((ImagCtteListener) it.next()).taskDiagramSwitched(event);
        }
    }
	
}
